<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/2/13
 * Time: 14:39
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Pp extends Base
{
    /**
     * 首页
     */
    public function index(){
        if(request()->isAjax()){
            extract(input());
            $where['t_id'] = ['>',0];
            if(isset($user_name) && $user_name != ''){
                $where['pp_name|pp_remark'] = ['like', '%' . $user_name . '%'];
            }
            if(isset($t_id) && $t_id != ''){
                $where['pp_t_id'] = $t_id;
            }
            if(isset($pp_status) && $pp_status != ''){
                $where['pp_status'] = $pp_status;
            }
            if(session('agid') == 10){
                $where['pp_id'] = session('uid');
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_pp p')
                ->join('up_types t', 'p.pp_t_id = t.t_id', 'left')
                ->where($where)
                ->count();// 获取总条数
            $field = 'p.*,t_name';
            $lists = Db::table('up_pp p')
                ->join('up_types t', 'p.pp_t_id = t.t_id', 'left')
                ->field($field)
                ->where($where)
                ->page($NowPage,$limits)
                ->order('pp_status asc,pp_sort desc,pp_time desc')
                ->select();
            if($lists){
                foreach ($lists as $k=>$v){
                    if($v['pp_two_pic']){
                        $lists[$k]['pp_two_pic'] = explode(',',$v['pp_two_pic']);
                    }
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $types = Db::name('types')
            ->field('t_id,t_name')
            ->order('t_sort desc')
            ->select();
        $this->assign('types',$types);
        return $this->fetch();
    }
    /**
     * 添加
     * @return mixed|\think\response\Json
     */
    public function newAdd(){
        if(Request::instance()->isPost()){
            extract(input());
            $data = [
                'pp_name' => $pp_name,
                'pp_thumb' => $pp_thumb,
                'pp_t_id' => $pp_t_id,
                'pp_desc' => $pp_desc,
//                'pp_images' => $pp_images,
                'pp_coupon1' => $pp_coupon1,
                'pp_coupon2' => $pp_coupon2,
                'pp_coupon3' => $pp_coupon3,
                'pp_coupon4' => $pp_coupon4,
                'pp_slogan' => $pp_slogan,
                'pp_two_pic' => $pp_two_pic,
                'pp_yongjin1' => $pp_yongjin1,
                'pp_yongjin2' => $pp_yongjin2,
                'pp_yongjin3' => $pp_yongjin3,
                'pp_yongjin4' => $pp_yongjin4,
//                'pp_star' => $pp_star,
//                'pp_coupon_remark' => $pp_coupon_remark,
                'pp_sort' => $pp_sort,
                'pp_phone' => $pp_phone,
                'pp_pass' => $pp_pass,
                'pp_time' => date('Y-m-d H:i:s')
            ];
//            if(isset($tens) && $tens){
//                foreach ($tens as $k => $v){
//                    $data['ten' . $v] = 1;
//                }
//            }
            if($pp_phone){
                if(!$pp_pass) return json(['code' => 100, 'msg' => '密码不能为空']);
                $have = Db::table('up_pp')->where('pp_phone',$pp_phone)->find();
                $have2 = Db::table('up_admin')
                    ->where('username',$pp_phone)
                    ->find();
                if($have || $have2) return json(['code' => 100, 'msg' => '已经有此账号了']);
            }
            $str = trim($pp_name,' ');
            $str= iconv("UTF-8","gb2312//IGNORE", $str);
            $tmp = bin2hex(substr($str,0,1));
            if($tmp>='B0'){ //汉字的开始
                $t=$this->getLetter(hexdec(bin2hex(substr($str,0,2))));
                $letter = chr($t);
            }else{
                $letter = strtoupper(substr($str,0,1));
            }
            $letter = $this->getLetter2($letter,mb_substr(trim($pp_name,' '),0,1));
            $data['pp_letter'] = $letter;
            $res = Db::table('up_pp')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $types = Db::name('types')
            ->field('t_id,t_name')
            ->order('t_sort desc')
            ->select();
        $tens = Db::name('ten')
            ->field('ten_id,ten_name')
            ->order('ten_status asc,ten_sort desc')
            ->select();
        $this->assign([
            'types' => $types,
            'tens' => $tens,
        ]);
        return $this->fetch('pp/newadd');
    }
    /**
     * 编辑
     * @return mixed
     */
    public function newEdit(){
        if(Request::instance()->isPost()){
            extract(input());
            $data = [
                'pp_name' => $pp_name,
                'pp_thumb' => $pp_thumb,
                'pp_desc' => $pp_desc,
//                'pp_images' => $pp_images,
                'pp_coupon1' => $pp_coupon1,
                'pp_coupon2' => $pp_coupon2,
                'pp_coupon3' => $pp_coupon3,
                'pp_coupon4' => $pp_coupon4,
                'pp_slogan' => $pp_slogan,
                'pp_two_pic' => $pp_two_pic,
                'pp_yongjin1' => $pp_yongjin1,
                'pp_yongjin2' => $pp_yongjin2,
                'pp_yongjin3' => $pp_yongjin3,
                'pp_yongjin4' => $pp_yongjin4,
//                'pp_star' => $pp_star,
//                'pp_coupon_remark' => $pp_coupon_remark,
                'pp_phone' => $pp_phone,
                'pp_pass' => $pp_pass,
                'pp_time' => date('Y-m-d H:i:s')
            ];
            if(session('agid') != 10){
                $data['pp_t_id'] = $pp_t_id;
                $data['pp_sort'] = $pp_sort;
            }
//            for($i=1;$i<=10;$i++){
//                if(isset($tens) && $tens && in_array($i,$tens)){
//                    $data['ten' . $i] = 1;
//                }else{
//                    $data['ten' . $i] = 2;
//                }
//            }
            if($pp_phone){
                if(!$pp_pass) return json(['code' => 100, 'msg' => '密码不能为空']);
                $have = Db::table('up_pp')
                    ->where('pp_phone',$pp_phone)
                    ->where('pp_id','<>',$pp_id)
                    ->find();
                $have2 = Db::table('up_admin')
                    ->where('username',$pp_phone)
                    ->find();
                if($have || $have2) return json(['code' => 100, 'msg' => '已经有此账号了']);
            }
            $str = trim($pp_name,' ');
            $str= iconv("UTF-8","gb2312//IGNORE", $str);
            $tmp = bin2hex(substr($str,0,1));
            if($tmp>='B0'){ //汉字的开始
                $t=$this->getLetter(hexdec(bin2hex(substr($str,0,2))));
                $letter = chr($t);
            }else{
                $letter = strtoupper(substr($str,0,1));
            }
            $letter = $this->getLetter2($letter,mb_substr(trim($pp_name,' '),0,1));
            $data['pp_letter'] = $letter;
            $res = Db::table('up_pp')->where('pp_id',$pp_id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '成功']);
            }else{
                return json(['code' => 100, 'msg' => '失败']);
            }
        }
        $pp_id = input('pp_id');
        $info = Db::name('pp')
            ->where('pp_id',$pp_id)
            ->find();
        $types = Db::name('types')
            ->field('t_id,t_name')
            ->order('t_sort desc')
            ->select();
        $tens = Db::name('ten')
            ->field('ten_id,ten_name')
            ->order('ten_status asc,ten_sort desc')
            ->select();
        $open = [];
        for($i=1;$i<=10;$i++){
            if($info['ten' . $i] == 1){
                $open[] = $i;
            }
        }
        $this->assign([
            'types' => $types,
            'tens' => $tens,
            'open' => $open,
            'info' => $info
        ]);
        return $this->fetch('pp/newedit');
    }
    
    /**
     * 删除
     * @return \think\response\Json
     */
    public function newDel(){
        if(session('agid') == 10){
            return json(['code' => 100, 'msg' =>' 您无权删除品牌']);
        }
        $new_id = input('id');
        $have = Db::table('up_store')->where('s_pp_id',$new_id)->find();
        if($have){
            return json(['code' => 100, 'msg' => '此品牌下有门店，不能删除，请先更改门店所属']);
        }
        $res = Db::table('up_pp')->where('pp_id',$new_id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    /**
     * 排序编辑
     * @return mixed|\think\response\Json
     */
    public function updateSort(){
        if(session('agid') == 10){
            return json(['code' => 100, 'msg' =>' 您无权修改排序顺序']);
        }
        $id = input('id');
        $field = input('field');
        $param = [
            $field => input('value'),
        ];
        $res = Db::name('pp')->where('pp_id',$id)->update($param);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 修改失败']);
        }
    }
    /**
     * 冻结启用
     * @return mixed|\think\response\Json
     */
    public function changeStatus(){
        if(session('agid') == 10){
            return json(['code' => 100, 'msg' =>' 您无权冻结']);
        }
        $id = input('id');
        $param = [
            'pp_status'=>input('num'),
        ];
        $res = Db::name('pp')->where('pp_id',$id)->update($param);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 修改失败']);
        }
    }
    /**
     * 批量批量
     * @return mixed|\think\response\Json
     */
    public function ppll(){
        $id = input('id');
        $field = input('field');
        $param = [
            $field=>input('value'),
        ];
        $res = Db::table('up_pp')->where('pp_id','in',$id)->update($param);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 修改失败']);
        }
    }
    //-------------------------------------------------轮播图------------------------------------------------
    /**
     * 轮播图首页展示
     * @return mixed|\think\response\Json
     */
    public function newBanner(){
        if(request()->isAjax ()){
            extract(input());
            $where = [
                'pb_where' => ['<>',5],
                'pb_pp_id' => session('pb_pp_id')
            ];
            if(isset($keyword) && $keyword != ''){
                $where['pb_title|pb_title_up'] = ['like', '%' . $keyword . '%'];
            }
            if(isset($banner_type) && $banner_type != "")
            {
                $where['pb_type'] = $banner_type;
            }
            if(isset($pb_where) && $pb_where != "")
            {
                $where['pb_where'] = $pb_where;
            }
            if(isset($pb_info_top) && $pb_info_top != "")
            {
                $where['pb_info_top'] = $pb_info_top;
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('pp_banner')->where($where)->count();// 获取总条数
            $lists = Db::name('pp_banner')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('pb_where asc,pb_sort desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $pp_id = input('pp_id');
        session('pb_pp_id',$pp_id);
        return $this->fetch('pp/banner');
    }
//    public function bannerAdd(){
//        if(Request::instance()->isPost()){
//            extract(input());
//            $data = [
//                'pb_title' => $pb_title,
//                'pb_pp_id' => session('pb_pp_id'),
//                'pb_type' => $pb_type,
//                'pb_sort' => $pb_sort,
//                'pb_where' => $pb_where,
//                'pb_time' => date('Y-m-d H:i:s')
//            ];
//            if($pb_type == 1){
//                $data['pb_url'] = $pb_url;
//            }else{
//                $data['pb_url'] = $video;
//            }
//            $res = Db::table('up_pp_banner')->insert($data);
//            if($res){
//                return json(['code' => 200, 'msg' => '添加成功']);
//            }else{
//                return json(['code' => 100, 'msg' => '添加失败']);
//            }
//        }
//        return $this->fetch('pp/banneradd');
//    }
    /**
     * 添加
     * @return mixed|\think\response\Json
     */
    public function bannerAdd(){
        if(Request::instance()->isPost()){
            extract(input());
            $cpd_content1 = trim($cpd_content1,', ');
            if(!$cpd_content1){
                return json(['code' => 100, 'msg' => '上传内容不能为空！']);
            }
            $cpd_content1 = explode(',',$cpd_content1);
            $data = [];
            foreach ($cpd_content1 as $k => $v){
                $mww = [
                    'pb_title' => $pb_title,
                    'pb_pp_id' => session('pb_pp_id'),
                    'pb_type' => $pb_type,
                    'pb_sort' => $pb_sort,
                    'pb_where' => $pb_where,
                    'pb_url' => $v,
                    'pb_time' => date('Y-m-d H:i:s')
                ];
                if($pb_type == 1){
                    $mww['pb_title_up'] = $pb_title_up;
                }
                if($pb_type == 3){
                    $mww['pb_url_h5'] = $pb_url_h5;
                }
                $data[] = $mww;
            }
            $res = Db::table('up_pp_banner')->insertAll($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $type = input('type');
        $this->assign('type',$type);
        return $this->fetch('pp/banneradd');
    }
    /**
     * 编辑
     * @return mixed
     */
    public function bannerEdit(){
        if(Request::instance()->isPost()){
            extract(input());
            $data = [
                'pb_title' => $pb_title,
                'pb_where' => $pb_where,
                'pb_time' => date('Y-m-d H:i:s')
            ];
            $pb_type = Db::table('up_pp_banner')->where('pb_id',$pb_id)->value('pb_type');
            if($pb_type != 2){
                $data['pb_url'] = $pb_url;
            }else{
                $data['pb_url'] = $video;
            }
            if($pb_type == 3){
                $data['pb_url_h5'] = $pb_url_h5;
            }
            if($pb_type == 1){
                $data['pb_title_up'] = $pb_title_up;
            }
            $res = Db::table('up_pp_banner')->where('pb_id',$pb_id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '成功']);
            }else{
                return json(['code' => 100, 'msg' => '失败']);
            }
        }
        $pb_id = input('pb_id');
        $info = Db::name('pp_banner')
            ->where('pb_id',$pb_id)
            ->find();
        $this->assign([
            'banner' => $info,
        ]);
        return $this->fetch('pp/banneredit');
    }
    /**
     * 删除
     * @return \think\response\Json
     */
    public function bannerDel(){
        $nt_id = input('id');
        $res = Db::table('up_pp_banner')->where('pb_id',$nt_id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    /**
     * 批量删除
     * @return \think\response\Json
     */
    public function batchDel(){
        $nt_id = input('ids');
        $res = Db::table('up_pp_banner')->where('pb_id','in',$nt_id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    /**
     * 设置图片
     * @return \think\response\Json
     */
    public function batchSetTwoPics(){
//        if(session('agid') == 10){
//            return json(['code' => 100, 'msg' =>' 您无权设置']);
//        }
        $nt_id = input('ids');
        if(!$nt_id) return json(['code' => 100, 'msg' => '请选择数据！']);
        $nt_id = explode(',',trim($nt_id,','));
        if(count($nt_id) > 2) return json(['code' => 100, 'msg' => '最多只能选择两条！']);

        $have = Db::table('up_pp_banner')
            ->where('pb_id','in',$nt_id)
            ->where('pb_type','in','2,3')
            ->find();
        if($have) return json(['code' => 100, 'msg' => '只能选择图片类型的！']);
        $data = Db::table('up_pp_banner')
            ->where('pb_id','in',$nt_id)
            ->find();
        $pics = Db::table('up_pp_banner')
            ->where('pb_id','in',$nt_id)
            ->column('pb_url');
        $pics = implode($pics,',');
        Db::table('up_pp')
            ->where('pp_id',$data['pb_pp_id'])
            ->setField('pp_two_pic',$pics);
        return json(['code' => 200, 'msg' => '启用成功']);
    }
    /**
     * 排序编辑
     * @return mixed|\think\response\Json
     */
    public function updateBannerSort(){
        $id = input('id');
        $param = [
            'pb_sort'=>input('value'),
        ];
        $res = Db::name('pp_banner')->where('pb_id',$id)->update($param);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 修改失败']);
        }
    }


    //-------------------------------------------------企业资料------------------------------------------------
    /**
     * 企业资料首页展示
     * @return mixed|\think\response\Json
     */
    public function ppFiles(){
        if(request()->isAjax ()){
            extract(input());
            $where = [
                'pb_pp_id' => session('pb_pp_id')
            ];
            if(isset($keyword) && $keyword != ''){
                $where['pb_title'] = ['like', '%' . $keyword . '%'];
            }
            if(isset($banner_type) && $banner_type != "")
            {
                $where['pb_type'] = $banner_type;
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('pp_file')->where($where)->count();// 获取总条数
            $lists = Db::name('pp_file')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('pb_sort desc,pb_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $pp_id = input('pp_id');
        session('pb_pp_id',$pp_id);
        return $this->fetch('pp/files');
    }
    /**
     * 添加
     * @return mixed|\think\response\Json
     */
    public function fileAdd(){
        if(Request::instance()->isPost()){
            extract(input());
            $data = [
                'pb_title' => $pb_title,
                'pb_pp_id' => session('pb_pp_id'),
                'pb_type' => $pb_type,
                'pb_sort' => $pb_sort,
                'pb_time' => date('Y-m-d H:i:s')
            ];
            if($pb_type == 1){
                $data['pb_url'] = $pb_url;
                $data['pb_desc'] = $pb_desc;
                $data['pb_bottom'] = $pb_bottom;
                $data['pb_thumb'] = $pb_thumb;
            }else{
                $data['pb_url'] = $pb_url2;
            }
            $res = Db::table('up_pp_file')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('pp/fileadd');
    }

//    public function fileAdd(){
//        if(Request::instance()->isPost()){
//            extract(input());
//            $cpd_content1 = trim($cpd_content1,', ');
//            if(!$cpd_content1){
//                return json(['code' => 100, 'msg' => '上传内容不能为空！']);
//            }
//            $cpd_content1 = explode(',',$cpd_content1);
//            $data = [];
//            foreach ($cpd_content1 as $k => $v){
//                $data[] = [
//                    'pb_title' => $pb_title,
//                    'pb_pp_id' => session('pb_pp_id'),
//                    'pb_type' => $pb_type,
//                    'pb_sort' => $pb_sort,
//                    'pb_where' => $pb_where,
//                    'pb_url' => $v,
//                    'pb_time' => date('Y-m-d H:i:s')
//                ];
//            }
//            $res = Db::table('up_pp_banner')->insertAll($data);
//            if($res){
//                return json(['code' => 200, 'msg' => '添加成功']);
//            }else{
//                return json(['code' => 100, 'msg' => '添加失败']);
//            }
//        }
//        return $this->fetch('pp/fileadd');
//    }
    /**
     * 编辑
     * @return mixed
     */
    public function fileEdit(){
        if(Request::instance()->isPost()){
            extract(input());
            $data = [
                'pb_title' => $pb_title,
                'pb_sort' => $pb_sort,
                'pb_time' => date('Y-m-d H:i:s')
            ];
            $pb_type = Db::table('up_pp_file')->where('pb_id',$pb_id)->value('pb_type');
            if($pb_type == 1){
                $data['pb_url'] = $pb_url;
                $data['pb_desc'] = $pb_desc;
                $data['pb_bottom'] = $pb_bottom;
                $data['pb_thumb'] = $pb_thumb;
            }else{
                $data['pb_url'] = $pb_url2;
            }
            $res = Db::table('up_pp_file')->where('pb_id',$pb_id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '成功']);
            }else{
                return json(['code' => 100, 'msg' => '失败']);
            }
        }
        $pb_id = input('pb_id');
        $info = Db::name('pp_file')
            ->where('pb_id',$pb_id)
            ->find();
        $this->assign([
            'info' => $info,
        ]);
        return $this->fetch('pp/fileedit');
    }
    /**
     * 删除
     * @return \think\response\Json
     */
    public function fileDel(){
        $nt_id = input('id');
        $res = Db::table('up_pp_file')->where('pb_id',$nt_id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    /**
     * 排序编辑
     * @return mixed|\think\response\Json
     */
    public function updateFileSort(){
        $id = input('id');
        $param = [
            'pb_sort'=>input('value'),
        ];
        $res = Db::name('pp_file')->where('pb_id',$id)->update($param);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 修改失败']);
        }
    }
    /**
     * 推荐
     * @return \think\response\Json
     */
    public function changeMainStatus(){
        $id = input('id');
        $param = [
            'pb_info_top'=>input('num'),
        ];
        $res = Db::table('up_pp_banner')->where('pb_id',$id)->update($param);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 修改失败']);
        }
    }
}